//
//  OBDBluetoothSendDataTool.swift
//  OBD
//
//  Created by sqkj on 2021/10/11.
//

import UIKit


//全局变量，方便调用
let ODBBLESendDataManager = OBDBluetoothSendDataTool.share

class OBDBluetoothSendDataTool: NSObject {
    
    var sendData:Data?
    var isPrintLog:Bool = true
    /// 当前协议发送消息ID
    var sendProtocolMsgID:[UInt8] = []
    
    typealias sendDataBlock = (_ data: Data) -> Void
    
    static let share: OBDBluetoothSendDataTool = OBDBluetoothSendDataTool()
}

extension OBDBluetoothSendDataTool {
    /// https://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift
    static func hexString(with data: Data) -> String {
        return data.map{ String(format: "%02x", $0)}.joined(separator: "")
    }
    
    static func data(with hexString: String) -> Data? {
        var data = Data(capacity: hexString.count / 2)
        let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive)
        regex.enumerateMatches(in: hexString, range: NSMakeRange(0, hexString.utf16.count)) { match, flags, stop in
            let byteString = (hexString as NSString).substring(with: match!.range)
            var num = UInt8(byteString, radix: 16)!
            data.append(&num, count: 1)
        }
        guard data.count > 0 else { return nil }
        return data
    }
}


extension OBDBluetoothSendDataTool{
  
}

 
